<?php
declare (strict_types=1);

namespace app\listener;

class Sale
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event)
    {

        if(isset($event[0]) && is_array($event[0])){
            foreach ($event as $events){

                $model = \app\model\Sale::where('product_id', $events['product_id'])->find();
                if (!$model) {
                    \app\model\Sale::create($events);
                } else {
                    $model->page_view = empty($events['page_view']) ? $model->page_view : $events['page_view'] + $model->page_view;
                    $model->sale = empty($events['sale']) ? $model->sale : $events['sale'] + $model->sale;
                    $model->num = empty($events['num']) ? $model->num : $events['num'] + $model->num;
                    $model->force()->save();
                }
            }

        }else{
            $model = \app\model\Sale::where('product_id', $event['product_id'])->find();
            if (!$model) {
                \app\model\Sale::create($event);
            } else {
                $model->page_view = empty($event['page_view']) ? $model->page_view : $event['page_view'] + $model->page_view;
                $model->sale = empty($event['sale']) ? $model->sale : $event['sale'] + $model->sale;
                $model->num = empty($event['num']) ? $model->num : $event['num'] + $model->num;
                $model->force()->save();
            }
        }

    }
}
